<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    // ES5 传统的对象取值  => 对象上取值 给对应的变量赋值
    // var zhang = {
    //     name: "张三",
    //     age: 18,
    //     height: "177cm",
    //     weight: "67kg",
    // }

    // var name = zhang.name; //从对象中取值 对变量进行赋值
    // var age = zhang.age; //从对象中取值 对变量进行赋值
    // var height = zhang.height; //从对象中取值 对变量进行赋值
    // var weight = zhang.weight; //从对象中取值 对变量进行赋值

    // ES6 允许按照一定模式，从数组和对象中提取值，对变量进行赋值，这被称为解构（Destructuring）。
    // 本质上，这种写法属于“模式匹配”，只要等号两边的模式相同，左边的变量就会被赋予对应的值

    // 对象解构 => 对应键名解构
    var zhang = { name: "张三", age: 18, height: "177cm", weight: "67kg" }

    // (1) 直接对应键名解构
    var { name, age, height, weight } = { name: "张三", age: 18, height: "177cm", weight: "67kg" };
    console.log(name, age, height, weight);

    // 等价于
    // var { "name": name, "age": age, "height": height, "weight": weight } = { name: "张三", age: 18, height: "177cm", weight: "67kg" };
    // console.log(name, age, height, weight);


    // (2) 自定义变量名
    // a. 如果对象的属性名是 关键词(class),不能直接对应键名解构 => 自定义变量名
    // b. 如果变量名 已经被使用  -> 自定义变量名

    // var { "name": a, "age": b, "height": c, "weight": d } = { name: "张三", age: 18, height: "177cm", weight: "67kg" };
    // console.log(a, b, c, d);

    var { name, age, class: _class, height, weight } = { name: "张三", age: 18, class: "2217", height: "177cm", weight: "67kg" };
    console.log(name, age, _class, height, weight);



</script>

</html>